﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using Manager.Common;
using System.Linq;
using System.Data.Linq;
using Manager.FormAdd_Edit;

namespace Manager.FormMain
{
    public partial class frm_DanhSachTamUng : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();

        public frm_DanhSachTamUng()
        {
            InitializeComponent();
        }

        private void frm_DanhSachTamUng_Load(object sender, EventArgs e)
        {
            db.Refresh(RefreshMode.OverwriteCurrentValues, db.TamUngs);
            gridControl1.DataSource = db.TamUngs.ToList();
        }

        private void butThemMoi_Click(object sender, EventArgs e)
        {
            frm_TamUng frm = new frm_TamUng();
            frm.ShowDialog();
            frm_DanhSachTamUng_Load(null, null);
        }

        private void butHieuChinh_Click(object sender, EventArgs e)
        {
            int index = gridView1.FocusedRowHandle;
            var tamUng = gridView1.GetRow(index) as TamUng;

            frm_TamUng frm = new frm_TamUng(tamUng);
            frm.ShowDialog();
            frm_DanhSachTamUng_Load(null, null);
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    int index = gridView1.FocusedRowHandle;
                    var tamUng = gridView1.GetRow(index) as TamUng;
                    tamUng.DichVu.SoLuongTamUng -= tamUng.SoLuongTamUng;
                    db.TamUngs.DeleteOnSubmit(tamUng);
                    db.SubmitChanges();
                    MessageBox.Show("Xóa dữ liệu thành công!");
                    this.frm_DanhSachTamUng_Load(null, null);
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("butXoa_Click " + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }
    }
}